package reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * 反射中使用注释
 */
public class ReflectDemo8 {
    public static void main(String[] args) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException, InstantiationException {

        //加载Person的类对象
        Class cls = Class.forName("reflect.Person");
        /**
         * boolean isAnnotationPresent(Class annolCls)
         *   检查是否有被annoCls指定的注释修饰
         */
        if (cls.isAnnotationPresent(AutoRunClass.class)) {
            System.out.println(cls.getName() + ":被@AutoRunClass修饰");
            Object obj = cls.newInstance();
            //扫描当前类定义的所有方法
            Method[] methods = cls.getDeclaredMethods();
            for (Method method : methods) {
                if (method.isAnnotationPresent(TestMethod.class)) {
                    System.out.println(method.getName()+":被@TestMethod修饰");
                    /**getAnnotation():通过方法获取注解@TestMethod*/
                    TestMethod tm = method.getAnnotation(TestMethod.class);
                    //获取参数value的值
                    int mou = tm.value();
                    for(int i=0;i<mou;i++){
                        method.invoke(obj);
                    }
                }else {
                    System.out.println(method.getName() + ":不被@TestMethod修饰");
                }
            }
        } else {
            System.out.println(cls.getName() + ":不被@AutRunClass修饰");
        }
    }
}
